﻿@model IQueryable<BasiliskBugTracker.WebClient.Models.BugViewModel>

@{
    ViewBag.Title = "Bugs";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2 class="well info">Bugs in project "@ViewBag.ProjectName"</h2>
<div class="info">
    <button class="btn btn-primary" id="btn-create">Report bug</button>
</div>
<br />
@(Html.Kendo().Grid(Model)
    .Name("bugs-grid")
    .Columns(columns =>
    {
        columns.Bound(b => b.Title);
        columns.Bound(b => b.AssignedTo).ClientTemplate("#: AssignedTo.Name#").Title("Assigned to").EditorTemplateName("ProjectContributors");
        columns.Bound(b => b.Owner).ClientTemplate("#: Owner.Name#").Title("Owner");
        columns.Bound(b => b.Status);
        columns.Bound(b => b.DateDiscovered).Format("{0:MMMM d, yyyy hh:mm}");
        columns.Bound(b => b.Description).ClientTemplate(string.Format("{0}...", "#: formatString(Description) #"));
        columns.Bound(b => b.Priority);
        columns.Bound(b => b.Attachments).ClientTemplate("<div class='attachments-view'></div>");
        columns.Bound(b => b.Project).ClientTemplate("#: Project.Id#").Hidden();
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
    })
    .Pageable()
    .Sortable()
         .Events(ev => ev.DataBound("getAttachments"))
    .Editable(m => m.Mode(GridEditMode.InLine))
    .Filterable()
    .DataSource(dataSource => dataSource
            .Ajax()
            .Events(events => events.Error("error_handler"))
            .Model(model =>
            {
                model.Id(b => b.Id);
                model.Field(b => b.Id).Editable(false);
                model.Field(b => b.Owner).Editable(false);
            })
            .Read(read => read.Action("Read", "Projects", new { id = ViewBag.ProjectId }))
            .Update(update => update.Action("Edit", "Bugs"))
            .Destroy(delete => delete.Action("Delete", "Bugs"))
            .PageSize(5)
            )
)

<br />
<button id="btn-back-projects" class="btn btn-primary"><i class="icon-white icon-th"></i> Back to projects</button>
@(Html.Kendo().Window()
        .Name("create")
        .Title("Report bug").LoadContentFrom("Create", "Bugs", new { id = ViewBag.ProjectId })

        .Draggable()
        .Resizable().Visible(false)
        .Width(600).Position(x => { x.Top(200); x.Left(300); })
        .Actions(actions => actions.Close())
)

<script type="text/javascript">
    $(document).ready(function () {

        var kendoWindow = $("#create").data("kendoWindow");

        $("#btn-create").bind("click", function () {
            kendoWindow.open();
        });

        kendoWindow.bind("refresh", onValidate);

        $("#btn-back-projects").bind("click", function () {
            window.location = "/Administration/Projects/Index";
        })
    });
    function error_handler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert(message);
        }
    }
    function formatString(value) {
        return value.substring(0, 20);
    }
    function onUpload(e) {
        // Array with information about the uploaded files
        var files = e.files;

        // Check the extension of each file and abort the upload if it is not .jpg
        $.each(files, function () {
            if (this.extension != ".zip") {
                alert("This file type is not supported. Only .zip files can be uploaded.")
                e.preventDefault();
                return false;
            }
        });
    }
    var onValidate = function (e) {
        $.validator.unobtrusive.parse($("#form-create-bug"));
        var isValid = $("#form-create-bug").valid();
        if (!isValid) {
            e.abort();
        }
    };

</script>
<script type="text/javascript">
    function getAttachments() {
        $.each($('.attachments-view'), function () {
            var grid = $('#bugs-grid').data().kendoGrid;
            var row = $(this).closest('tr');
            var dataItem = grid.dataItem(row);
            if (dataItem.Attachments && dataItem.Attachments.length > 0) {
                $(this).kendoListView({
                    dataSource: { data: dataItem.Attachments },
                    template: kendo.template($("#template").html())
                });

            } else {
                $(this).html("<a href='#' class='btn btn-info btn-mini disabled'><i class='icon-white icon-remove'></i> No attachments</a>");
            }
        });
    }
</script>
<script type="text/x-kendo-template" id="template">
    <a href="#: data#" class="btn btn-primary btn-mini"><i class="icon-white icon-hdd"></i> Download</a>
</script>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

